'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  //导入书籍页面
  async import_booklet() {
    const { ctx } = this;
    await ctx.render('import_booklet.html');
  }

  //后台入口页面
  async index() {
    const { ctx } = this;
    await ctx.render('index.html');
  }

  //用户管理入口页面
  async user() {
    const { ctx } = this;
    const { query } = ctx
    const { page = 1, limit = 7 } = query
    const {data: {data: {users, userCount}}} = await ctx.curl(`http://localhost:3001/back_api/v1.0/users?limit=${limit}&page=${page}`, {
      dataType: 'json',
    });
    await ctx.render('user.html', {
      users,
      userCount,
      limit,
      page
    });
  }

  //用户书籍页面
  async user_booklet() {
    const { ctx , service } = this;
    const { query } = ctx
    const { user_id } = query
    const userBookletRecords = await service.booklet.findBookletByUserId(user_id)
    const booklets = userBookletRecords.map(function(userBookletRecord) {
      return userBookletRecord.booklet_model.toJSON()
    })

    await ctx.render('user_booklet.html', {
      booklets,
      bookletsCount: booklets.length
    });
  }
}

module.exports = HomeController;